package ntd.util.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tool.ToolSpring;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class UtilPage {

    public static <T> Page<T> defaultPage() {
        HttpServletRequest request = ToolSpring.getRequest();
        //每页多少条数据
        Integer limit = Integer.valueOf(ObjectUtil.defaultIfBlank(request.getParameter("limit"), "15"));
        //第几页
        Integer pageNum = Integer.valueOf(ObjectUtil.defaultIfBlank(request.getParameter("page"), "1"));
        final Page<T> page = new Page(pageNum, limit);
        final String order = request.getParameter("order");
        final String sort = request.getParameter("sort");
        if(StringUtils.isNoneBlank(order, sort)){
            if(order.equals("desc")){
                page.addOrder(OrderItem.desc(sort));
            } else {
                page.addOrder(OrderItem.asc(sort));
            }
        }
        return page;
    }

}
